package com.mashang.servlet.clazz;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mashang.domain.Clazz;
import com.mashang.util.ResultView;
import com.mashang.vo.clazz.ClazzViewVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/class/name")
public class ClazzAllListServlet extends HttpServlet {

    public static List<Clazz> classViewList = new ArrayList<>();

    static {
        classViewList.add(new Clazz(1100l, "高一一班", "2001", "2023-12-13 23:22:21", "在一楼", "1"));
        classViewList.add(new Clazz(1101l, "高一二班", "2002", "2022-12-13 23:22:21", "在二楼", "2"));

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<ClazzViewVo> clazzViewVoList = new ArrayList<>();
        //将Clazz对象转成ClazzViewVo类型对象
        for (Clazz clazz : ClazzAllListServlet.classViewList) {
            ClazzViewVo viewVo = new ClazzViewVo();
            viewVo.setClassId(clazz.getClassId());
            viewVo.setName(clazz.getName());

            clazzViewVoList.add(viewVo);
        }


        ResultView<List<ClazzViewVo>> resultView = new ResultView();
        resultView.setData(clazzViewVoList);

        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(resultView);

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(result);

    }
}
